<?php
/**
 * JAVA接口拉取会话消息
 */
namespace app\msgapi\service;

use app\utils\Http;
error_reporting(E_ERROR | E_PARSE);
class JavaPullMsgApiService
{
    private $apiUrl;
    private $corpid;
    private $secret;

    private Http $http;

    public function __construct()
    {
        $corpInfo = HscorpInfoService::getCorpInfo(env('wework.corpid'),true); //获取企业密钥信息
        $this->apiUrl = 'http://127.0.0.1:1992/';
        $this->corpid = $corpInfo['corp_info']['corpid'];
        $this->secret = $corpInfo['corp_info']['msg_secret'];
        $this->http = new Http(['base_uri' => $this->apiUrl]);
    }

    /**初始化SDK
     * @param $corpid
     * @param $secret
     * @return array|bool|mixed|string
     * @throws \Exception
     */
    public function initSdk()
    {
        $url = "txchat/initSdk";
        $data = [
            'sdkId' => env('wework.corpid') . time() . rand(11, 99),
            'corpid' => $this->corpid,
            'secrectkey' => $this->secret
        ];
        return $this->http->request('GET',$url,$data);
    }

    /**获取会话信息并解密
     * @param $sdkId
     * @param $seq
     * @param $pk
     * @param int $limit
     * @return array|bool|mixed|string
     * @throws \Exception
     */
    public function getDecryptChatData($sdkId, $seq, $pk, $limit = 1000)
    {
        $url = "txchat/getDecryptChatData";
        $data = [
            'sdkId' => $sdkId,
            'seq' => $seq,
            'limit' => $limit,
            'timeout' => 50,
            'pk' => $pk,
        ];
        return $this->http->request('GET',$url,$data);
    }

    /**通过私钥解密数据
     * @param $sdkId
     * @param $pk
     * @param $encryptRandomKey
     * @param $encryptChatMsg
     * @return array|bool|mixed|string
     * @throws \Exception
     */
    public function decryptDataByPk($sdkId,$pk,$encryptRandomKey,$encryptChatMsg){
        $url = 'txchat/decryptDataByPk';
        $data = [
            'sdkId'=>$sdkId,
            'pk'=>$pk,
            'encryptRandomKey'=>$encryptRandomKey,
            'encryptChatMsg'=>$encryptChatMsg
        ];
        return $this->http->request('GET',$url,$data);
    }

    /**销毁SDKID
     * @param $sdkId
     * @return array|bool|mixed|string
     * @throws \Exception
     */
    public function destroySdk($sdkId)
    {
        $url = "txchat/destroySdk";
        $data = [
            'sdkId' => $sdkId
        ];
        return $this->http->request('GET',$url,$data);
    }

    /**获取媒体资料
     * @param $sdkId
     * @param $indexbuf
     * @param $sdkfileid
     * @return array|bool|mixed|string
     * @throws \Exception
     */
    public function getMediaData($sdkId, $indexbuf, $sdkfileid)
    {
        $url = "txchat/getMediaData";
        $data = [
            'sdkId' => $sdkId,
            'indexbuf' => $indexbuf,
            'sdkfileid' => $sdkfileid,
            'timeout' => 10000
        ];
        return $this->http->request('GET',$url,$data);
    }

}